﻿//Created by Action Script Viewer - http://www.buraks.com/asv
package net.wooga.pets.utils.timeline {
    import flash.display.*;
    import flash.errors.*;
    import flash.events.*;
    
    import net.wooga.pets.events.*;

    public class SequenceBasedStatesTimeline extends EventDispatcher {

        private const FRAME_LABEL_IS_NOT_FOUND_ON_TIMELINE:FrameLabel = null;
        private const IS_NOT_SET:Object = null;

        private var _timeline:MovieClip;
        private var _stopAtFrameIndex:int;
        private var _isPlaying:Boolean;
        private var _convertLabelToIndexTable:Array;
        private var _currentPlayingSequence:StateTimelineSequence;

        public function SequenceBasedStatesTimeline(_arg1:MovieClip){
            this._convertLabelToIndexTable = [];
            super();
            this._timeline = _arg1;
            this.verifyHasLabels();
        }
        private function verifyHasLabels():void{
            if (this._timeline.currentLabels.length == 0){
                throw (new IllegalOperationError("the timeline should at least contain one frame label"));
            };
        }
        public function destroy():void{
            this.interrupt();
            this._timeline = null;
            this._convertLabelToIndexTable = null;
        }
        public function get isPlaying():Boolean{
            return (this._isPlaying);
        }
        public function gotoAndStop(_arg1:Object):void{
            this.interrupt();
            this._timeline.gotoAndStop(_arg1);
        }
        public function playFromFrameLabel(_arg1:String):void{
            this.interrupt();
            this.playSequence(this.getSequenceByFrameLabel(_arg1));
        }
        private function hasSequenceByLabel(_arg1:String):Boolean{
            return (!((this._convertLabelToIndexTable[_arg1] == this.IS_NOT_SET)));
        }
        private function getSequenceByFrameLabel(_arg1:String):StateTimelineSequence{
            if (!this.hasSequenceByLabel(_arg1)){
                this._convertLabelToIndexTable[_arg1] = this.parseLabel(_arg1);
            };
            return (this._convertLabelToIndexTable[_arg1]);
        }
        private function playSequence(_arg1:StateTimelineSequence):void{
            this._currentPlayingSequence = _arg1;
            this._stopAtFrameIndex = this._currentPlayingSequence.stopIndex;
            this._timeline.gotoAndStop(this._currentPlayingSequence.startIndex);
            this._timeline.play();
            this._timeline.addEventListener(Event.ENTER_FRAME, this.checkCurrentIndexIsStopIndex);
            this._isPlaying = true;
        }
        private function parseLabel(_arg1:String):StateTimelineSequence{
            var _local5:StateTimelineSequence;
            var _local6:FrameLabel;
            var _local8:int;
            var _local9:Boolean;
            var _local10:int;
            var _local2:int = this._timeline.totalFrames;
            var _local3:int = this._timeline.currentLabels.length;
            var _local4:uint = 1;
            var _local7:int;
            while (_local7 < _local3) {
                _local6 = this._timeline.currentLabels[_local7];
                if (_local6.name == _arg1){
                    _local5 = new StateTimelineSequence();
                    _local5.sequenceId = _arg1;
                    _local5.startIndex = _local6.frame;
                    _local8 = (_local7 + 1);
                    _local9 = (this._timeline.currentLabels[_local8] == null);
                    _local10 = (_local9) ? _local2 : (this._timeline.currentLabels[_local8].frame - _local4);
                    _local5.stopIndex = _local10;
                    break;
                };
                _local7++;
            };
            if (_local5 == null){
                throw (new Error((((("There is no frame label defined [" + _arg1) + "] in the asset with linkage name: [") + this._timeline.name) + "]!")));
            };
            return (_local5);
        }
        private function checkCurrentIndexIsStopIndex(_arg1:Event):void{
            if (this._timeline.currentFrame >= this._stopAtFrameIndex){
                this.interrupt();
                this.onTimelineAnimationFinished();
            };
        }
        public function interrupt():void{
            if (this.isPlaying){
                this._timeline.stop();
                this._isPlaying = false;
                this._timeline.removeEventListener(Event.ENTER_FRAME, this.checkCurrentIndexIsStopIndex);
            };
        }
        private function onTimelineAnimationFinished():void{
            var _local1:String = this._currentPlayingSequence.sequenceId;
            this.dispatchEvent(new InfoEvent(Event.COMPLETE, "", {sequence_label:_local1}));
        }
        public function stop():void{
            this._timeline.stop();
        }

    }
}//package net.wooga.pets.utils.timeline 

class StateTimelineSequence {

    public var startIndex:int;
    public var stopIndex:int;
    public var sequenceId:String;

    public function StateTimelineSequence(){
    }
}
